package com.fengxu.gamescript.custom

import android.animation.ArgbEvaluator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.util.AttributeSet
import android.view.View
import androidx.core.animation.doOnEnd
import com.scwang.smartrefresh.layout.util.DensityUtil
import com.xuexiang.xui.utils.DensityUtils
import org.jetbrains.anko.backgroundColor

/**
 * 波纹效果控件
 */
class RingView : View {

    // 该组件的半径
    var radius = DensityUtils.dp2px(5F)

    // 圆环的宽度
    var ringWidth = DensityUtils.dp2px(1F)

    // 圆环的颜色
    var ringColor = Color.BLACK

    // 圆环渐变后的颜色
    var ringEndColor = Color.WHITE

    // 做圆环动画的最大半径
    var maxRadius = 2*radius

    // 该组件的最小size
    var minSize = DensityUtil.dp2px(10F)

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val widthMode = MeasureSpec.getMode(widthMeasureSpec)
        val widthSize = MeasureSpec.getSize(widthMeasureSpec)
        val heightMode = MeasureSpec.getMode(heightMeasureSpec)
        val heightSize = MeasureSpec.getSize(heightMeasureSpec)
        var realWidth = 0
        var realHeight = 0
        if(widthMode == MeasureSpec.EXACTLY){
            realWidth = widthSize
        }else{
            realWidth = (radius + ringWidth) * 2 + paddingTop + paddingBottom
        }
        if(heightMode == MeasureSpec.EXACTLY){
            realHeight = heightSize
        }else{
            realHeight = (radius + ringWidth) * 2 + paddingLeft + paddingRight
        }
        setMeasuredDimension(realWidth, realHeight)
    }

    override fun onDraw(canvas: Canvas?) {
        val paint = Paint()
        paint.strokeWidth = ringWidth.toFloat()
        paint.style = Paint.Style.STROKE
        paint.color = ringColor
        canvas!!.drawCircle(x + width/2, y + height/2, radius.toFloat(),paint)
    }

    /**
     * 显示圆环动画
     * @param time 显示时间
     * @param onEnd 动画完成时的事件
     */
     fun showAnim(time: Int,  onEnd:(()->Unit)? = null){
        // 两个动画完成的标志
        val animFinishArr: BooleanArray = booleanArrayOf(false, false)
        // 动画全部完成时触发
        val animEnd = {
            if(animFinishArr.all { it }){
                onEnd?.invoke()
            }
        }
        // 半径动画
        val anim1 = ValueAnimator.ofInt(radius, maxRadius).apply {
            duration = time.toLong()
            addUpdateListener {
                radius = it.animatedValue as Int
                requestLayout()
                invalidate()
            }
            doOnEnd {
                animFinishArr[0] = true
                animEnd()
            }
        }
        // 颜色动画
        val anim2 = ValueAnimator.ofInt(ringColor,ringEndColor).apply {
            duration = time.toLong()
            setEvaluator(ArgbEvaluator())
            addUpdateListener {
                ringColor = it.animatedValue as Int
                invalidate()
            }
            doOnEnd {
                animFinishArr[1] = true
                animEnd()
            }
        }
        // 开启动画
        anim1.start()
        anim2.start()
    }



}